﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Mission
{
    class MainMissionFactory
    {
        static List<IMainMissionHandler> list = new List<IMainMissionHandler>();

        internal static void register(IMainMissionHandler handler)
        {
            list.Add(handler);
        }

        public static List<Entities.Usr_Mainmission> handle(string userCode, List<Entities.Usr_Mainmission> missions)
        {
            List<Entities.Usr_Mainmission> doneList = new List<Entities.Usr_Mainmission>();

            foreach (Entities.Usr_Mainmission mission in missions)
            {
                LoggerHelper.Debug("try handle main mission. user:{0} mission:{1}",//
                        mission.Usercode, mission.Missionserialcode);

                if (mission.Missionstatus == GeneralBillStatusCollection.FINISH)
                    continue;

                foreach (IMainMissionHandler handler in list)
                {
                    if (!handler.meet(mission))
                        continue;

                    if (!handler.handle(userCode, mission))
                        continue;

                    LoggerHelper.Debug("end finish main mission. user:{0} mission:{1} handle:{2}",//
                             mission.Usercode, mission.Missionserialcode, handler.ToString());

                    mission.Missionstatus = (GeneralBillStatusCollection.FINISH);

                    doneList.Add(mission);
                }
            }

            return doneList;
        }

    }
}
